package com.njxzc.igo.controller.address;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import com.njxzc.igo.model.User;
import com.njxzc.igo.service.AddressFacade;
import com.njxzc.igo.service.AddressService;
import com.njxzc.igo.service.AddressServiceImpl;
import com.njxzc.igo.utils.JsonUtil;
import com.njxzc.igo.utils.ShoppingResult;

import net.sf.json.JSONObject;

@WebServlet(name = "addAddress", urlPatterns = "/addAddress.do")
public class AddAddressController extends HttpServlet {
	private AddressService as = new AddressServiceImpl();

	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		
		String receivername = request.getParameter("receivername");
		String phone = request.getParameter("phone");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String region = request.getParameter("region");
		String details = request.getParameter("details");
		User u = (User) session.getAttribute("u");
		
		ShoppingResult rs = as.addNewAddress(u.getUsername(), receivername, phone, province, city, region, details);
		
		Set addresses = as.LoadAllAddress(u.getUsername());
		u.setAddresses(addresses);
		JSONObject jo = JSONObject.fromObject(rs,JsonUtil.solveAddresslazy());
		out.write(jo.toString());
	}

}
